package com.lzh.springboot.rabbitmqdemo.config;

import com.lzh.springboot.rabbitmqdemo.message.Demo01Message;
import com.lzh.springboot.rabbitmqdemo.message.Demo02Message;
import com.lzh.springboot.rabbitmqdemo.message.Demo03Message;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: LiuZiHao
 * @Date: 2022/5/6 14:46
 */
@Configuration
public class RabbitConfig {

    /**
     * Direct Exchange 示例的配置类
     */
    public static class DirectExchangeDemoConfiguration {
        //创建Queue
        @Bean
        public Queue demo01Queue() {
            return new Queue(Demo01Message.QUEUE,//Queue 名字
                    true, // durable 是否持久化
                    false, //exclusive 是否排他
                    false);// autoDelete 是否自动删除
        }

        //创建 Direct Exchange
        @Bean
        public DirectExchange demo01Exchange() {
            return new DirectExchange(Demo01Message.EXCHANGE,
                    true, // 是否持久化
                    false); // 是否排他
        }

        //创建 Binding
        //QUEUE = "QUEUE_DEMO_01";
        //EXCHANGE = "EXCHANGE_DEMO_01";
        //ROUTING_KEY = "ROUTING_KEY_01";
        @Bean
        public Binding demo01Binding() {
            return BindingBuilder.bind(demo01Queue()).to(demo01Exchange()).with(Demo01Message.ROUTING_KEY);
        }

    }

    /**
     * Topic Exchange 示例的配置类
     */
    public static class TopicExchangeDemoConfiguration {

        //创建Queue
        @Bean
        public Queue demo02Queue() {
            return new Queue(Demo02Message.QUEUE,
                    true,
                    false,
                    false);
        }

        //创建 Topic Exchange
        @Bean
        public TopicExchange demo02Exchange() {
            return new TopicExchange(Demo02Message.EXCHANGE,
                    true,
                    false);
        }

        //创建Binding
//        QUEUE = "QUEUE_DEMO_02";
//        EXCHANGE = "EXCHANGE_DEMO_02";
//        ROUTING_KEY = "#.yu.nai";
        @Bean
        public Binding demo02Binding() {
            return BindingBuilder.bind(demo02Queue()).to(demo02Exchange()).with(Demo02Message.ROUTING_KEY);
        }
    }

    /**
     * Fanout Exchange 示例的配置类
     */
    public static class FanoutExchangeDemoConfiguration {

        @Bean
        public Queue demo03QueueA() {
            return new Queue(Demo03Message.QUEUE_A,
                    true,
                    false,
                    false);
        }

    }
}
